package com.test.controllers;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpHeaders;
import org.springframework.http.RequestEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

/**
 * .
 *
 * @author: bcc
 * 2021/10/27
 */
@RestController
@RequestMapping("/http")
public class HttpController {

    /**
     * logger.
     */
    private static final Logger LOGGER = LoggerFactory.getLogger(HttpController.class);

    @GetMapping("/get")
    public Map<String, Object> get(String param1, String param2) {
        Map<String, Object> rs = new HashMap<>();
        rs.put("result", "success");
        LOGGER.info("param1:{},param2:{}", param1, param2);
        return rs;
    }
    @GetMapping("/sleep")
    public Map<String, Object> get(Integer sleep, String config) {
        if (null == sleep) {
            sleep = 3;
        }
        LOGGER.info("start sleep:{},config:{}", sleep, config);
        try {
            Thread.sleep(sleep * 1000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        Map<String, Object> rs = new HashMap<>();
        rs.put("result", sleep);
        LOGGER.info("finish sleep:{},config:{}", sleep, config);
        return rs;
    }


    @PostMapping("/post")
    public Map<String, Object> post(String param1, String param2) {
        Map<String, Object> rs = new HashMap<>();
        rs.put("result", "success");
        LOGGER.info("param1:{},param2:{}", param1, param2);
        return rs;
    }

    @PostMapping("/post1")
    public Map<String, Object> post1(@RequestBody(required = false) Map<String, Object> param) {
        Map<String, Object> rs = new HashMap<>();
        rs.put("result", "success");
        LOGGER.info("param:{}", param);
        return rs;
    }


    @PostMapping("/post2")
    public Map<String, Object> post2(@RequestBody(required = false)String param, HttpServletRequest request) {
        Map<String, Object> rs = new HashMap<>();
        rs.put("result", "success");
        LOGGER.info("param:{}", param);
        for(String head: Collections.list(request.getHeaderNames())) {
            LOGGER.info("header:{} value:{}", head, request.getHeader(head));
        }
        return rs;
    }

    @PostMapping("/post3")
    public Map<String, Object> post3(@RequestParam("param") String param, HttpServletRequest request) {
        Map<String, Object> rs = new HashMap<>();
        rs.put("result", "success");
        LOGGER.info("param:{}", param);
        for(String head: Collections.list(request.getHeaderNames())) {
            LOGGER.info("================header:{} value:{}================", head, request.getHeader(head));
        }
        return rs;
    }

    @PostMapping("/testRequestEntity")
    public String testRequestEntity(RequestEntity<String> entity){
        //获取请求报文的请求头信息
        HttpHeaders httpHeaders = entity.getHeaders();
        for (String head : httpHeaders.keySet()) {
            LOGGER.info("================header:{} value:{}====================", head, httpHeaders.get(head));
        }
        //)获取请求报文的请求体信息
        String body = entity.getBody();
        LOGGER.info("=======================body:{}", body);
        return "success";
    }
}
